﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;
using Atc600Control;
using Trio.Motor;

namespace RollController2022
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }
        Atc600 Atc600 = new Atc600();
        TrioP600 TrioP600 = new TrioP600();
        private void Form4_Load(object sender, EventArgs e)
        {
            //Atc600.Connect();
            //TrioP600.Connect();
            ////TrioP600.RollInitMpos(0);
            //TrioP600.wdog();
            //timer1.Start();
            //getvalue_timer.Interval = 50;
            //getvalue_timer.Elapsed += new ElapsedEventHandler(value_Elapsed);
        }

        private void value_Elapsed(object sender, ElapsedEventArgs e)
        {
            Encoder = TrioP600.GetEncoder(0);
            //label2.Text = Encoder.ToString();
            if (Encoder == 10)
            {
                TrioP600.RollMove(0, 10, -10);//走线轴8与方向前后
            }
            else if (Encoder == 0)
            {
                TrioP600.RollMove(0, 10, 10);//走线轴8与方向前后
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Atc600.SetTension(0, (int)numericUpDown1.Value);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            //Atc600.SetTension(1, (int)(10 * numericUpDown2.Value));
        }

        private void Form4_FormClosed(object sender, FormClosedEventArgs e)
        {
            //Atc600.Closed();
            timer1.Stop();
        }
        System.Timers.Timer getvalue_timer = new System.Timers.Timer();//位置速度取值定时器
        double Encoder;
        int i = 0;
        bool timer1num = false;
        private void timer1_Tick(object sender, EventArgs e)
        {
            i++;
            label2.Text = i.ToString();
            if (i == 100)
                timer1num = true;
            //label2.Text = Atc600.read_tension().ToString();
            //Encoder = TrioP600.GetEncoder(0);
            //label2.Text = Encoder.ToString();

            ////Encoder = (Encoder / 200 * 4);
            ////label2.Text = Encoder.ToString();

            //if (Encoder > 12800)
            //{
            //    TrioP600.CtrlSpeed(0, 0);
            //}
        }

        private void button3_Click(object sender, EventArgs e)
        {
            numericUpDown2.Maximum = 10;

            //timer1.Start();

            //System.Threading.Thread.Sleep(50);
            //i++;
            label2.Text = numericUpDown2.Value.ToString();
            //if (i == 100)
            //    return;
            //timer1.Stop();
            //i = 0;


            //TrioP600.RoollInitMotor(0, 0);//走线轴7与方向
            //TrioP600.RollMove(0, 10, 10);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //TrioP600.RollInitMpos(0);
            //TrioP600.RoollInitMotor(0, 1);//走线轴7与方向
            TrioP600.RollMove(0, 10, -10);
        }

        double e1, e2;
        private void button3_MouseDown(object sender, MouseEventArgs e)
        {
            //TrioP600.RoollInitMotor(0, 0);//走线轴7与方向
            //TrioP600.CtrlSpeed(0, 100);
            //e1 = Math.Abs(TrioP600.GetEncoder(0));
            i++;
            label2.Text = i.ToString();
        }

        private void button3_MouseUp(object sender, MouseEventArgs e)
        {
            //TrioP600.RoollInitMotor(0, 0);//走线轴7与方向
            //TrioP600.CtrlSpeed(0, 0);
            //label2.Text = ((Math.Abs(TrioP600.GetEncoder(0)) - e1) / 200 * 4).ToString();
        }

        private void button4_MouseDown(object sender, MouseEventArgs e)
        {
            //TrioP600.RoollInitMotor(0, 1);//走线轴7与方向
            //TrioP600.CtrlSpeed(0, 100);
            //e1 = Math.Abs(TrioP600.GetEncoder(0));
        }

        private void button5_Click(object sender, EventArgs e)
        {
            getvalue_timer.Start();//打开定时器
        }

        private void numericUpDown3_ValueChanged(object sender, EventArgs e)
        {
            label3.Text = Math.Round((double)numericUpDown3.Value, 1).ToString();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            label4.Text = ((double)numericUpDown3.Value).ToString();
        }

        private void button4_MouseUp(object sender, MouseEventArgs e)
        {
            //TrioP600.RoollInitMotor(0, 1);//走线轴7与方向
            //TrioP600.CtrlSpeed(0, 0);
            //label2.Text = ((Math.Abs(TrioP600.GetEncoder(0)) - e1) / 200 * 4-5).ToString();
        }
    }
}
